package com.kqb.sgauth.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.hutool.core.bean.BeanUtil;
import com.kqb.sgauth.model.vo.MiniProgramLoginVO;
import com.kqb.sgauth.service.UserAccountService;
import com.kqb.sgauth.service.WxService;
import com.kqb.sgcommon.base.response.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 杉果树皮
 */
@RestController
@Slf4j
@RequestMapping("/public/wx")
public class WxController {

    @Resource
    private WxService wxService;

    @Resource
    private UserAccountService userAccountService;

    @GetMapping("/token")
    public Result<MiniProgramLoginVO> token(@RequestParam("code") String code) {
        SaTokenInfo saTokenInfo = wxService.wxLogin(code);
        MiniProgramLoginVO miniProgramLoginVO = BeanUtil.copyProperties(saTokenInfo, MiniProgramLoginVO.class);
        return Result.success(miniProgramLoginVO);
    }

    @GetMapping("/updateAvatar")
    public Result<?> updateAvatar(@RequestParam("avatarUrl") String avatarUrl) {
        userAccountService.updateUserAvatar(avatarUrl);
        return Result.success();
    }

    @GetMapping("/updateNickName")
    public Result<?> updateNickName(@RequestParam("nickName") String nickName) {
        userAccountService.updateUserNickName(nickName);
        return Result.success();
    }

    @GetMapping("/updatePhone")
    public Result<?> updatePhone(@RequestParam("phone") String phone) {
        userAccountService.updateUserPhone(phone);
        return Result.success();
    }
}
